package com.my.lab.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.my.lab.R;
import com.my.lab.util.ConfigConstant;

public class TcpBroadcastActivity extends Activity {
    
    TextView tv ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.son_layout);
        tv = (TextView) findViewById(R.id.tv);
        BrocastThread bt = new BrocastThread();
        bt.start();
        Callback cb = new Callback();
        getThread gt = new getThread(cb);
        gt.start();
    }
    
    
    
    public class BrocastThread extends Thread {
        @Override
        public void run() {
            String sendtext = "123";
            try {
                final DatagramSocket ds = new DatagramSocket(ConfigConstant.UDP_BOADCASTPORT);
                byte[] databyte = new byte[1024];
                databyte = sendtext.getBytes("utf-8");
                DatagramPacket sendDp = new DatagramPacket(databyte, databyte.length,
                        InetAddress.getByName("255.255.255.255"), ConfigConstant.UDP_BOADCASTPORT);
                ds.send(sendDp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public class getThread extends Thread {
        
        Callback c;
        public getThread(Callback c) {
            this.c = c;
        }
        
        @Override
        public void run() {
            try {
                final DatagramSocket rds = new DatagramSocket(ConfigConstant.UDP_SEND);
                byte[] buf = new byte[ConfigConstant.WIFI_BUFFER_LENGTH];
                DatagramPacket receiveDp = new DatagramPacket(buf, ConfigConstant.WIFI_BUFFER_LENGTH);
                rds.receive(receiveDp);
                if(receiveDp.getLength() > 0) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            tv.setText("收到应答，开始传输文件");
                        }
                    });
                    c.start(ConfigConstant.apkfile , receiveDp.getAddress().getHostAddress());
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            
        }
    }
    
    
    public class Callback {
        
        void start(File file , String ip) {
            sendThread st = new sendThread(file, ip);
            st.start();
        }
    }

    public class  sendThread extends Thread{
        
        private Socket mSocket  = null ;
        private OutputStream os = null;
        private InputStream is = null ;
        private FileInputStream fis = null ;
        File file ;
        String ip ;
        public sendThread(File file ,  String ip ) {
            this.file = file ;
            this.ip = ip ;
        }
        
        @Override
        public void run() {
            try {
                mSocket = new Socket(InetAddress.getByName(ip) , ConfigConstant.TCP_CONNECTPORT );
                os = mSocket.getOutputStream();
                fis = new FileInputStream(file);
                
                int filelenth = (int) file.length();
                int left = filelenth;
                int sendOnce = 0 ;
                byte buffer[] = new byte[ConfigConstant.WIFI_BUFFER_LENGTH];
                while(left > 0) {
                    sendOnce = fis.read(buffer);
                    os.write(buffer, 0, sendOnce);
                    os.flush();
                    left = left - sendOnce;
                    Thread.sleep(500);
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    mSocket.close();
                    os.close();
                    is.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        }
        
    }
}